﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sponsorship.Domain;
using Raven.Client;
using Sponsorship.Domain.PresentTemplates;
using Sponsorship.ReadModel.Persons;
using Sponsorship.ReadModel.Sponsors;

namespace Sponsorship.ReadModel.Presents
{
    public class PresentTemplateListItemHandler :
        IEventHandler<PresentTemplateEntered>,
        IEventHandler<PresentTemplateChanged>
    {
        private readonly IDocumentStore m_documentStore;

        public PresentTemplateListItemHandler(IDocumentStore docStore)
        {
            m_documentStore = docStore;
        }


        public void Handle(PresentTemplateEntered domainEvent)
        {
            using (var session = m_documentStore.OpenSession())
            {
                
                var dto = new PresentTemplateListItemDto
                {
                    AggregateRootId = domainEvent.PresentTemplateId,

                    Comment = domainEvent.Comment,
                    Amount = domainEvent.PresentData.Amount,
                    Description = domainEvent.PresentData.Description,

                };

                session.SaveChanges(dto, domainEvent);
            }
        }

        public void Handle(PresentTemplateChanged domainEvent)
        {
            using (var session = m_documentStore.OpenSession())
            {
                var presentTemplate = session.LoadById<PresentTemplateListItemDto>(domainEvent.PresentTemplateId);

                presentTemplate.Comment = domainEvent.Comment;
                presentTemplate.Amount = domainEvent.PresentData.Amount;
                presentTemplate.Description = domainEvent.PresentData.Description;

                session.SaveChanges(presentTemplate, domainEvent);
            }
        }



    }
}
